/**
 * create by young (young2f@outlook.com) on 2020/9/15
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Button, Form, Popconfirm, Table, Tabs} from 'antd';
import styles from '../index.less';
import {ModalView} from 'antd-extra';

import ViewTitle from '../../components/ViewTitle';
import ComponentBase from '../../components/Base/ComponentBase';
import notify from "../../components/NotificationExtra";
import ModalCreateDict from "./ModalCreateDict";

const {TabPane} = Tabs;
const _ = require('lodash');
@Form.create()
@connect(({global, user, dict, dynamicLabel}) => ({...global, ...user, ...dict, ...dynamicLabel}))
export default class DictMgr extends ComponentBase {
  constructor(props) {
    super(props);

  }

  componentDidMount() {
    const {dispatch} = this.props;
    dispatch && dispatch({
      type: 'dict/findMany', payload: {type: 'division', tenantCode: this.getTenantCode()}
    });
    dispatch && dispatch({
      type: 'dict/findMany', payload: {type: 'industry', tenantCode: this.getTenantCode()}
    });
  }


  addLabel = (type) => {
    ModalView.open(ModalCreateDict, {title: '编辑', width: '720px', height: '80%', noStore: true}, {
      type,
      dispatch: this.props.dispatch,
      callback: (data) => {
        data.type = type;
        this.props.dispatch({
          type: 'dict/create',
          payload: data
        }).then(res => {
          //    this.requestReturn(res);
          this.componentDidMount();
        });
      }
    });

  };

  deleteLabel = record => {
    const {code} = record;
    this.props.dispatch({
      type: 'dict/deleteOne',
      payload: {code}
    }).then(res => {
      this.requestReturn(res);
      this.componentDidMount();
    });
  };

  columns = () => {
    const columns = [
      {title: '编码', dataIndex: 'code', key: 'code'},
      {title: '名称', dataIndex: 'name', key: 'dict'},
      {
        title: '操作',
        key: 'operation',
        render: (value, record) => {
          const {isInitKey, isUnique} = record;
          return (
            <span>
              {<Popconfirm
                title="确认删除"
                onConfirm={this.deleteLabel.bind(this, record)}
                okText="是"
                cancelText="否"
              >
                <a disabled={isInitKey}>删除</a>
              </Popconfirm>
              }
            </span>
          );
        }
      }
    ];
    return columns;
  };

  getTabs = type => {
    const dicts = this.props[`${type}Dict`];
    return (
      <div>
        {
          <ViewTitle
            title={`列表`}
            level={2}
            actions={<Button type="primary" onClick={this.addLabel.bind(this, type)}>添加</Button>}></ViewTitle>
        }
        <Table
          className="antTable"
          bordered={true}
          columns={this.columns()}
          dataSource={dicts}
          pagination={false}></Table>
      </div>
    );
  };

  render() {
    const {_notify} = this.props;
    if (_notify) {
      notify(_notify);
    }

    return (
      <div className={styles.content}>
        <Tabs defaultActiveKey="1">
          <TabPane tab="事业部" key="1">
            {this.getTabs('division')}
          </TabPane>
          <TabPane tab="客户行业" key="2">
            {this.getTabs('industry')}
          </TabPane>
        </Tabs>
      </div>
    );
  }
}
